package packhib;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Employe.
 * @see packhib.Employe
 * @author MyEclipse - Hibernate Tools
 */
public class EmployeDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(EmployeDAO.class);

	//property constants
	public static final String NOM = "nom";
	public static final String PRENOM = "prenom";
	public static final String CNSS = "cnss";
	public static final String CIN = "cin";
	public static final String ADRESSE = "adresse";
	public static final String SF = "sf";
	public static final String NBR_EFANT = "nbrEfant";
	public static final String TYPE_CONTRAT = "typeContrat";
	public static final String STATUT = "statut";
	public static final String POSTE = "poste";
	public static final String ACTIF = "actif";
	

    
    public void save(Employe transientInstance) {
        log.debug("saving Employe instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Employe persistentInstance) {
        log.debug("deleting Employe instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Employe findById( java.lang.Integer id) {
        log.debug("getting Employe instance with id: " + id);
        try {
            Employe instance = (Employe) getSession()
                    .get("packhib.Employe", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Employe instance) {
        log.debug("finding Employe instance by example");
        try {
            List results = getSession()
                    .createCriteria("packhib.Employe")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Employe instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Employe as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByNom(Object nom) {
		return findByProperty(NOM, nom);
	}
	
	public List findByPrenom(Object prenom) {
		return findByProperty(PRENOM, prenom);
	}
	
	public List findByCnss(Object cnss) {
		return findByProperty(CNSS, cnss);
	}
	
	public List findByCin(Object cin) {
		return findByProperty(CIN, cin);
	}
	
	public List findByAdresse(Object adresse) {
		return findByProperty(ADRESSE, adresse);
	}
	
	public List findBySf(Object sf) {
		return findByProperty(SF, sf);
	}
	
	public List findByNbrEfant(Object nbrEfant) {
		return findByProperty(NBR_EFANT, nbrEfant);
	}
	
	public List findByTypeContrat(Object typeContrat) {
		return findByProperty(TYPE_CONTRAT, typeContrat);
	}
	
	public List findByStatut(Object statut) {
		return findByProperty(STATUT, statut);
	}
	
	public List findByPoste(Object poste) {
		return findByProperty(POSTE, poste);
	}
	
	public List findByActif(Object actif) {
		return findByProperty(ACTIF, actif);
	}
	
	
	
    public Employe merge(Employe detachedInstance) {
        log.debug("merging Employe instance");
        try {
            Employe result = (Employe) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Employe instance) {
        log.debug("attaching dirty Employe instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Employe instance) {
        log.debug("attaching clean Employe instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public ArrayList<String> getNamesStartingWith(String nom) 
	   {
		   Session session=HibernateUtil.getSessionFactory().openSession();
			 Transaction Tx=session.beginTransaction();
			 ArrayList<String> listeDeNom;
			 listeDeNom = new ArrayList<String>();
			
			 String SQL_QUERY ="Select nom From Employe";
			 Query query = session.createQuery(SQL_QUERY);
	     //query:est une liste je peux la parcourir pour avoir ces element 
			 
			 
			 for(Iterator it=query.iterate();it.hasNext();)
			 {
				 
				 String chaine=(String)it.next();
				 
				 if(chaine.startsWith(nom))
				 {
					 listeDeNom.add(chaine);
				 }
			 }
			
			 return  listeDeNom;
	   }
    
    public List getNomEmployes() 
	   {
    	List listNames=new ArrayList<String>();;
    	Session session=HibernateUtil.getSessionFactory().openSession();
		 Transaction Tx=session.beginTransaction();
		 ArrayList<String> listeDeNom;
		 listeDeNom = new ArrayList<String>();
		
		 String SQL_QUERY ="Select nom From Employe";
		 Query query = session.createQuery(SQL_QUERY);
		 for(Iterator it=query.iterate();it.hasNext();)
		 {
			 String chaine=(String)it.next();
			 listNames.add(chaine);
			 
		 }
		 return listNames;
    	
	   }
}